package top.llove.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.llove.service.ArticleService;
import top.llove.utils.res.ApiResult;

/**
 * 文章Controller
 *
 * @author: 幸会
 * @date: 2021/6/17 17:57
 * @email: 1846724873@qq.com
 * @description: 文章Controller
 * version: 1.0
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService article; // 文章Service


    /**
     * 根据文章id准确查询文章，文章标题模糊查询，查询分类文章，查询管理员文章
     * @param id 文章id
     * @param title 文章标题
     * @param kind 文章分类
     * @param user 管理员id
     * @return
     */
    @GetMapping("/q")
    public ApiResult select(@RequestParam(value = "i", required = false) String id,
                            @RequestParam(value = "t", required = false) String title,
                            @RequestParam(value = "k", required = false) String kind,
                            @RequestParam(value = "u", required = false) String user) {
        return article.select(id, title == null ? null : "%" + title + "%", kind, user);
    }

    /**
     * 添加文章
     * @param title 文章标题
     * @param img 文章图片
     * @param desc 文章描述
     * @param context 文章内容
     * @param kind 文章分类
     * @param user 所属管理员
     * @return
     */
    @PostMapping("/xh/s")
    public ApiResult save(@RequestParam(value = "t", required = true) String title,
                          @RequestParam(value = "i", required = true) String img,
                          @RequestParam(value = "d", required = true) String desc,
                          @RequestParam(value = "c", required = true) String context,
                          @RequestParam(value = "k", required = true) String kind,
                          @RequestParam(value = "u", required = true) String user) {
        return article.save(title, img, desc, context, kind, user);
    }

    /**
     * 文章修改
     * @param title 文章标题
     * @param img 文章图片
     * @param desc 文章描述
     * @param context 文章内容
     * @param kind 文章分类
     * @param id 文章id
     * @return
     */
    @PostMapping("/xh/e")
    public ApiResult edit(@RequestParam(value = "t", required = true) String title,
                          @RequestParam(value = "i", required = true) String img,
                          @RequestParam(value = "d", required = true) String desc,
                          @RequestParam(value = "c", required = true) String context,
                          @RequestParam(value = "k", required = true) String kind,
                          @RequestParam(value = "a", required = true) String id) {
        return article.edit(title, img, desc, context, kind, id);
    }

    /**
     * 删除文章, 删除分类文章，删除管理员所有文章，根据文章id删除文章
     * @param id 文章id
     * @param user 管理员id
     * @param kind 分类id
     * @return
     */
    @GetMapping("/xh/d")
    public ApiResult delete(@RequestParam(value = "i", required = false) String id,
                            @RequestParam(value = "u", required = false) String user,
                            @RequestParam(value = "k", required = false) String kind) {
        return article.delete(id, user, kind);
    }
}
